package com.gome.gop.common;

import java.io.Serializable;
import java.util.List;

/**
 * 通用分页对象
 * 
 * @author user
 *
 * @param <T>
 */
public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private long total; // 总记录数
    
    private List<T> rows; // 结果集
    
    private int currentPage; // 第几页
    
    private int pageSize = 10; // 每页记录数
    
    private int pageTotal; // 总页数
    
    /**
     * 是否执行分页
     */
    private boolean limitPage = true;
    
    public static PageParam buildPageParam(int currentPage, int pageSize) {
        return new PageParam(currentPage, pageSize);
    }
    
    public static class PageParam {
        private Integer currentPage;
        
        private Integer pageSize;
        
        public PageParam(int currentPage, int pageSize) {
            this.currentPage = currentPage;
            this.pageSize = pageSize;
        }
        
        public Integer getCurrentPage() {
            return currentPage;
        }
        
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
        
        public Integer getPageSize() {
            return pageSize;
        }
        
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    }
    
    public Page(boolean limitPage) {
        this.limitPage = limitPage;
    }
    
    public Page(int pageSize, int currentPage) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }
    
    public static <T> Page<T> create(int pageSize, int currentPage) {
        return new Page<T>(pageSize, currentPage);
    }
    
    public static <T> Page<T> create() {
        return new Page<T>(1, 1);
    }
    
    public Page(int currentPage, int total, List<T> rows) {
        this.total = total;
        this.currentPage = currentPage;
        this.rows = rows;
    }
    
    public int startIndex() {
        int startindex = 0;
        if (currentPage > 0) {
            return (currentPage - 1) * pageSize;
        }
        return startindex;
    }
    
    public int endIndex() {
        int startindex = 0;
        if (currentPage > 0) {
            return currentPage * pageSize;
        }
        return startindex;
    }
    
    public long getTotal() {
        return total;
    }
    
    public void setTotal(long total) {
        this.total = total;
    }
    
    public int getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    public int getCurrentPage() {
        return currentPage;
    }
    
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    
    public int getPageTotal() {
        return pageTotal;
    }
    
    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }
    
    public boolean isLimitPage() {
        return limitPage;
    }
    
    public void setLimitPage(boolean limitPage) {
        this.limitPage = limitPage;
    }
    
    /**
     * @return the rows
     */
    
    public List<T> getRows() {
        return rows;
    }
    
    /**
     * @param rows the rows to set
     */
    
    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}